package com.xiaoy.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.PropertyFilter;

@Repository("daoUtil")
@Transactional
public class DaoUtil<T, PK extends Serializable> extends
		HibernateDaoWrapper<T, PK> {

	// private Session session = getSession();

	public List<T> query(Class<T> c, String sql, int start, int pagesize,
			List<PropertyFilter> filters) {
		Session session = getSession();
		SQLQuery query = session.createSQLQuery(sql);
		if (filters != null) {
			for (int i = 0; i < filters.size(); i++) {

				PropertyFilter pf = filters.get(i);
				query.setParameter(i, pf.getPropertyValue());
			}
		}
		query.addEntity(c);
		query.setMaxResults(pagesize).setFirstResult(start);
		return query.list();
	}

	public List<T> query(Class c, String sql) {
		Session session = getSession();
		SQLQuery query = session.createSQLQuery(sql);
		query.addEntity(c);
		return query.list();
	}

	// --------非实体映射
	
	public List<Map<String, Object>> query(String sql, Object[] params) {

		Session session = getSession();
		SQLQuery query = session.createSQLQuery(sql);
		for (int i = 0; i < params.length; i++) {
			Object obj = params[i];
			query.setParameter(i, obj);
		}
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		return query.list();
	}

	public List<Map<String, Object>> query(String sql) {
		return query(sql, new Object[] {});
	}
	
	public List<Map<String, Object>> query(String sql,Map<String, Object>params) {
		String where=" ";
		for (Entry<String, Object>entry:params.entrySet()) {
			where +=entry.getKey()+"="+entry.getValue();
		}
		sql+=where;
		return query(sql,params);
	}
	

	
	
	// 查询 start ---------------------------

	public List<Map<String, Object>> query(String sql, int start, int pagesize) {
		Session session = getSession();
		SQLQuery query = session.createSQLQuery(sql);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		query.setMaxResults(pagesize).setFirstResult(start);
		return query.list();
	}

	public List<Map<String, Object>> query(List<String> fields,String tablename, String where, String orderBy) {

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		StringBuilder sql = new StringBuilder();

		StringBuilder field = new StringBuilder("");
		for (String key : fields) {
			field.append(key + ",");
		}

		String f = field.toString();
		f = f.substring(0, f.length() - 1);

		sql.append("select ").append(f).append(" " + tablename + " where ")
				.append(where + " " + orderBy);

		String s = sql.toString();
		query(s);

		return list;
	}

	public Map<String, Object> queryForMap(String sql) {
		List<Map<String, Object>> list = query(sql);
		if (list.size() > 0) {
			return list.get(0);
		}
		return new HashMap<String, Object>();
	}

	public Map<String, Object> queryForMap(String sql, Object[] params) {
		List<Map<String, Object>> list = query(sql, params);
		if (list.size() > 0) {
			return list.get(0);
		}
		return new HashMap<String, Object>();
	}

	// 查询 end ---------------------------

	// 更新操作 start ---------------------------

	/**
	 * 执行更新操作的sql 语句 包括sava update delete
	 * 
	 * @param sql
	 */
	public int saveOrUpdate(String sql) {
		Session session = getSession();
		SQLQuery query = session.createSQLQuery(sql);
		return query.executeUpdate();
	}

	public int saveOrUpdate(String sql, List<Object> params) {

		return saveOrUpdate(sql, params.toArray());
	}

	public int saveOrUpdate(String sql, Object[] params) {
		Session session = getSession();
		SQLQuery query = session.createSQLQuery(sql);
		for (int i = 0; i < params.length; i++) {
			Object obj = params[i];
			query.setParameter(i, obj);
		}
		return query.executeUpdate();
	}

	public void saveOrUpdate(T t) {
		Session session = getSession();
		session.saveOrUpdate(t);
	}
	// 更新操作 end ---------------------------
}